﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: OrganizationUtils.cs
//* Revision: 1.0
//* Author: JiratB
//* Platform: .NET Framework 4.0
//* Description: Utilities for creating CRM organization service. This
//*   class also provides operations around the organization service also.
//* Note: Ada is my lovely daughter.
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20130119  JiratB       - First release to Codeplex
//************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//-- CRM References --
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk;
//--------------------

//-- System References --
using System.ServiceModel.Description;
//using System.IO;
//using System.Text.RegularExpressions;
//using System.Data;
//using System.Runtime.Serialization;
//using System.Data.Odbc;
//using System.Globalization;
//-----------------------

namespace AdaCRMUtilities
{
    public class OrganizationUtils
    {
        public IOrganizationService service;

        public string organization;
        public string crmServerName;
        public string crmServerPort;
        public string uriString;

        #region constructors
        public OrganizationUtils()
        {
        }

        public OrganizationUtils(string[] args)
        {
            Initialize(args);
        }
        #endregion

        protected void Initialize(string[] args)
        {
            if (args.Length < 4)
            {
                organization = "AdventureWorksCycles";
                crmServerName = "nyc-dc1";
                crmServerPort = "81";
            }//-- end of if --
            else
            {
                organization = args[2];
                crmServerName = args[0];
                crmServerPort = args[1];
            }//-- end of else --

            uriString = String.Format(
                "http://{0}:{1}/{2}/XRMServices/2011/Organization.svc",
                crmServerName,
                crmServerPort,
                organization
                );
        }//-- End of Initialize() --

        public IOrganizationService GetOrganization()
        {
            Uri organizationUri = new Uri(uriString);

            var credential = new ClientCredentials();

            OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(
                organizationUri,
                null,
                credential,
                null
                );

            service = (IOrganizationService)serviceProxy;

            return service;
        }//-- End of GetOrganization() --

    }//-- End of Class --
}//-- End of Namespace --

//************************************************************************
// End of file
//************************************************************************